package com.firefly.admin.config.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.firefly.admin.common.constant.Constants;
import com.firefly.admin.common.enums.ResultCodeEnum;
import com.firefly.admin.common.enums.SensitiveTypeEnum;
import com.firefly.admin.common.vo.Result;
import com.firefly.admin.monitor.vo.SelectResult;
import com.firefly.admin.config.service.FySystemSensitiveConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import com.firefly.admin.config.entity.FySystemSensitiveConfig;
import com.firefly.admin.config.mapper.FySystemSensitiveConfigMapper;
import com.firefly.admin.config.vo.SystemSensitiveConfigQueryVo;
import com.firefly.admin.config.vo.SystemSensitiveConfigStatusUpdateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class FySystemSensitiveConfigServiceImpl implements FySystemSensitiveConfigService {
    @Autowired
    private FySystemSensitiveConfigMapper fySystemSensitiveConfigMapper;

    /**
     * 通过敏感类型获取敏感配置
     *
     * @param type 敏感类型
     * @return 敏感配置列表
     */
    @Override
    public List<FySystemSensitiveConfig> selectListByType(String type) {
        LambdaQueryWrapper<FySystemSensitiveConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FySystemSensitiveConfig::getSensitiveType, type);
        queryWrapper.eq(FySystemSensitiveConfig::getConfigStatus, Constants.Status.ENABLE);
        return fySystemSensitiveConfigMapper.selectList(queryWrapper);
    }

    /**
     * 分页查询系统敏感数据配置接口
     *
     * @param systemSensitiveConfigQueryVo 系统敏感配置查询条件
     * @return 敏感配置列表
     */
    @Override
    public Result<Page<FySystemSensitiveConfig>> getSystemSensitiveConfigList(SystemSensitiveConfigQueryVo systemSensitiveConfigQueryVo) {
        log.info("###########FySystemSensitiveConfigServiceImpl getSystemSensitiveConfigList param = {}", JSONObject.toJSONString(systemSensitiveConfigQueryVo));
        LambdaQueryWrapper<FySystemSensitiveConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(systemSensitiveConfigQueryVo.getSensitiveType()), FySystemSensitiveConfig::getSensitiveType, systemSensitiveConfigQueryVo.getSensitiveType());
        queryWrapper.eq(StringUtils.isNotBlank(systemSensitiveConfigQueryVo.getConfigStatus()), FySystemSensitiveConfig::getConfigStatus, systemSensitiveConfigQueryVo.getConfigStatus());
        queryWrapper.eq(FySystemSensitiveConfig::getDeleteFlag, Constants.DeleteFlag.NORMAL);
        queryWrapper.orderByDesc(FySystemSensitiveConfig::getCreateTime);
        Page<FySystemSensitiveConfig> page = new Page<>(systemSensitiveConfigQueryVo.getPage(), systemSensitiveConfigQueryVo.getLimit());
        Page<FySystemSensitiveConfig> systemUserPage = fySystemSensitiveConfigMapper.selectPage(page, queryWrapper);
        return Result.success(systemUserPage);
    }

    /**
     * 系统敏感数据配置新增接口
     *
     * @param fySystemSensitiveConfig 系统敏感数据配置
     * @return 新增结果
     */
    @Override
    public Result<FySystemSensitiveConfig> add(FySystemSensitiveConfig fySystemSensitiveConfig) {
        if (fySystemSensitiveConfig == null) {
            return Result.getResult(ResultCodeEnum.REQUEST_PARAM_EXCEPTION);
        }
        if (StringUtils.isBlank(fySystemSensitiveConfig.getConfigStatus())) {
            fySystemSensitiveConfig.setConfigStatus(Constants.Status.DISABLE);
        }
        int rows = fySystemSensitiveConfigMapper.insert(fySystemSensitiveConfig);
        if (rows <= 0) {
            return Result.getResult(ResultCodeEnum.ADD_EXCEPTION);
        }
        return Result.success(fySystemSensitiveConfig);
    }

    /**
     * 系统敏感数据配置编辑接口
     *
     * @param fySystemSensitiveConfig 系统敏感数据配置
     * @return 编辑结果
     */
    @Override
    public Result<FySystemSensitiveConfig> update(FySystemSensitiveConfig fySystemSensitiveConfig) {
        if (fySystemSensitiveConfig == null) {
            return Result.getResult(ResultCodeEnum.REQUEST_PARAM_EXCEPTION);
        }
        Long configId = fySystemSensitiveConfig.getConfigId();
        if (configId == null) {
            return Result.getResult(ResultCodeEnum.REQUEST_PARAM_EXCEPTION);
        }
        FySystemSensitiveConfig systemSensitiveConfig = fySystemSensitiveConfigMapper.selectById(configId);
        if (systemSensitiveConfig == null) {
            return Result.getResult(ResultCodeEnum.SYSTEM_SENSITIVE_CONFIG_NOT_FOUND);
        }
        int rows = fySystemSensitiveConfigMapper.updateById(fySystemSensitiveConfig);
        if (rows <= 0) {
            return Result.getResult(ResultCodeEnum.UPDATE_EXCEPTION);
        }
        return Result.success(fySystemSensitiveConfig);
    }

    /**
     * 系统敏感数据配置状态修改接口
     *
     * @param systemSensitiveConfigStatusUpdateVo 系统敏感数据配置状态编辑参数
     * @return 操作结果
     */
    @Override
    public Result<FySystemSensitiveConfig> updateStatus(SystemSensitiveConfigStatusUpdateVo systemSensitiveConfigStatusUpdateVo) {
        if (systemSensitiveConfigStatusUpdateVo == null) {
            return Result.getResult(ResultCodeEnum.REQUEST_PARAM_EXCEPTION);
        }
        Long configId = systemSensitiveConfigStatusUpdateVo.getConfigId();
        if (configId == null) {
            return Result.getResult(ResultCodeEnum.REQUEST_PARAM_EXCEPTION);
        }
        FySystemSensitiveConfig systemSensitiveConfig = fySystemSensitiveConfigMapper.selectById(configId);
        if (systemSensitiveConfig == null) {
            return Result.getResult(ResultCodeEnum.SYSTEM_SENSITIVE_CONFIG_NOT_FOUND);
        }
        String configStatus = systemSensitiveConfigStatusUpdateVo.getConfigStatus();
        if (StringUtils.isBlank(configStatus)) {
            return Result.getResult(ResultCodeEnum.SYSTEM_SENSITIVE_CONFIG_STATUS_NOT_NULL);
        }
        systemSensitiveConfig.setConfigStatus(configStatus);
        int rows = fySystemSensitiveConfigMapper.updateById(systemSensitiveConfig);
        if (rows <= 0) {
            return Result.getResult(ResultCodeEnum.UPDATE_EXCEPTION);
        }
        return Result.success(systemSensitiveConfig);
    }

    /**
     * 系统敏感数据配置删除接口
     *
     * @param configId 配置ID
     * @return 删除结果
     */
    @Override
    public Result<String> delete(Long configId) {
        if (configId == null) {
            return Result.getResult(ResultCodeEnum.REQUEST_PARAM_EXCEPTION);
        }
        FySystemSensitiveConfig systemSensitiveConfig = fySystemSensitiveConfigMapper.selectById(configId);
        if (systemSensitiveConfig == null) {
            return Result.getResult(ResultCodeEnum.SYSTEM_SENSITIVE_CONFIG_NOT_FOUND);
        }
        systemSensitiveConfig.setDeleteFlag(Constants.DeleteFlag.DELETE);
        int rows = fySystemSensitiveConfigMapper.updateById(systemSensitiveConfig);
        if (rows <= 0) {
            return Result.getResult(ResultCodeEnum.DELETE_EXCEPTION);
        }
        return Result.success();
    }

    /**
     * 敏感类型枚举列表查询接口
     *
     * @return 敏感类型列表
     */
    @Override
    public Result<Map<String, List<SelectResult>>> getSensitiveTypeEnumList() {
        Map<String, List<SelectResult>> result = new HashMap<>();
        SensitiveTypeEnum[] sensitiveTypeEnums = SensitiveTypeEnum.values();
        List<SelectResult> sensitiveTypeList = new ArrayList<>();
        List<SelectResult> sensitiveTypeWithDisableList = new ArrayList<>();
        for (SensitiveTypeEnum sensitiveTypeEnum : sensitiveTypeEnums) {
            SelectResult selectResult = new SelectResult();
            String type = sensitiveTypeEnum.getType();
            selectResult.setName(sensitiveTypeEnum.getName());
            selectResult.setValue(type);
            sensitiveTypeList.add(selectResult);
            //包含不可用
            LambdaQueryWrapper<FySystemSensitiveConfig> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(FySystemSensitiveConfig::getSensitiveType, type);
            queryWrapper.eq(FySystemSensitiveConfig::getDeleteFlag, Constants.DeleteFlag.NORMAL);
            List<FySystemSensitiveConfig> sensitiveConfigList = fySystemSensitiveConfigMapper.selectList(queryWrapper);
            if (!CollectionUtils.isEmpty(sensitiveConfigList)) {
                selectResult.setDisabled(true);//当为新增时，已经配置过敏感类型不可再配置
            }
            sensitiveTypeWithDisableList.add(selectResult);
        }
        result.put("sensitiveTypeList", sensitiveTypeList);
        result.put("sensitiveTypeWithDisableList", sensitiveTypeWithDisableList);
        return Result.success(result);
    }
}
